package main.java.com.example.mainstream.database;

import android.annotation.SuppressLint;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;

import com.example.shopapp.bean.Pinglun;
import com.example.shopapp.dao.PinglunDao;

import java.util.ArrayList;
import java.util.List;


/**
 * 文件名：PinglunDBUtils
 * 描述：TOOD
 */
public class PinglunDBUtils {
    public static final String DB_NAME = "pinglun_dbname";

    public static final int VERSION = 1;

    private static PinglunDBUtils sqliteDB;

    private SQLiteDatabase db;

    private PinglunDBUtils(Context context) {

    }


    public synchronized static PinglunDBUtils getInstance(Context context) {
        if (sqliteDB == null) {
            sqliteDB = new PinglunDBUtils(context);
        }
        return sqliteDB;
    }



    /**
     * 插入数据
     * @param user
     */
    public void insert(Pinglun user) {
        PinglunDao pinglunDao = GreenDaoManager.getmInstance().getDaoSession().getPinglunDao();
        pinglunDao.insert(user);

    }
    /**
     * 查询所有数据
     * @return
     */
    @SuppressLint("Range")
    public List<Pinglun> findAllByLuntanId(String luntan_id){
        List<Pinglun> shopAddressList = new ArrayList<>();
        PinglunDao pinglunDao = GreenDaoManager.getmInstance().getDaoSession().getPinglunDao();
        List<Pinglun> list = pinglunDao.loadAll();
        if (list!=null && list.size() > 0){
            for (int i = 0; i < list.size(); i++) {
                if (luntan_id.equals(list.get(i).getLuntan_id())){
                    shopAddressList.add(list.get(i));
                }
            }
        }
        return shopAddressList;
    }

    @SuppressLint("Range")
    public int getToTalByLuntanId(String luntan_id){
        int TOTAL = 0;

        PinglunDao pinglunDao = GreenDaoManager.getmInstance().getDaoSession().getPinglunDao();
        List<Pinglun> list = pinglunDao.loadAll();
        if (list!=null && list.size() > 0){
            for (int i = 0; i < list.size(); i++) {
                if (luntan_id.equals(list.get(i).getLuntan_id())){
                    ++TOTAL;
                }
            }
        }
        return TOTAL;
    }
}
